package com.share.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import com.share.constants.Constants;
import com.share.domain.MyEmail;
import com.share.mapper.DriverMaterialAuthLogMapper;
import com.share.pojo.bo.DriverAuthBo;
import com.share.pojo.domain.DriverAuthMaterial;
import com.share.mapper.DriverAuthMaterialMapper;
import com.share.pojo.domain.DriverMaterialAuthLog;
import com.share.service.IDriverAuthMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.template.EmailTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author zcj
 * @since 2025-03-25
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {
    @Autowired
    private DriverMaterialAuthLogMapper driverMaterialAuthLogMapper;
    @Autowired
    private EmailTemplate emailTemplate;
    @Value("${spring.mail.username}")
    private String from;

    @Override
    public boolean save(DriverAuthMaterial entity) {
        entity.setCreateTime(new Date());
        entity.setId(IdUtil.createSnowflake(0L,0L).nextId());
        entity.setRealAuthStatus(Constants.Driver.REAL_AUTH_STATUS_0);
        return super.save(entity);
    }

    @Override
    @Transactional
    public Boolean auth(DriverAuthBo driverAuthBo) {
        //逻辑
        //根据id查询对应的审核数据
        Long id = driverAuthBo.getId();
        DriverAuthMaterial driverAuthMaterial = super.getById(id);
        //判断 通过还是不通过
        if(driverAuthBo.getApprove()){
            //通过 修改审核数据为通过
            driverAuthMaterial.setRealAuthStatus(Constants.Driver.REAL_AUTH_STATUS_1);
        }else {
            //不通过 修改审核数据为不通过
            driverAuthMaterial.setRealAuthStatus(Constants.Driver.REAL_AUTH_STATUS_2);
        }
        // 设置对应的审核人信息
        Date date = new Date();
        driverAuthMaterial.setAuditTime(date);
        driverAuthMaterial.setAuditUserId(StpUtil.getLoginIdAsLong());
        driverAuthMaterial.setAuditRemark(driverAuthBo.getRemark());
        super.updateById(driverAuthMaterial);
        //不管审核成功还是失败都要记录审核记录表
        DriverMaterialAuthLog log = createLog(driverAuthMaterial, date);
        driverMaterialAuthLogMapper.insert(log);
        //发送短信通知用户
        MyEmail myEmail = new MyEmail();
        myEmail.setFrom(from);
        myEmail.setTo("2971000838@qq.com");
        myEmail.setContent("恭喜你飞驰代驾注册成功");
        myEmail.setSubject("飞驰代驾");
        emailTemplate.sendEmail(myEmail);
        return true;
    }

    private static DriverMaterialAuthLog createLog(DriverAuthMaterial driverAuthMaterial, Date date) {
        DriverMaterialAuthLog driverMaterialAuthLog = new DriverMaterialAuthLog();
        driverMaterialAuthLog.setId(IdUtil.createSnowflake(0L,0L).nextId());
        driverMaterialAuthLog.setAuthMaterialId(driverAuthMaterial.getId());
        driverMaterialAuthLog.setRealAuthStatus(driverAuthMaterial.getRealAuthStatus());
        driverMaterialAuthLog.setAuditTime(date);
        driverMaterialAuthLog.setAuditUserId(driverAuthMaterial.getAuditUserId());
        driverMaterialAuthLog.setAuditRemark(driverAuthMaterial.getAuditRemark());
        driverMaterialAuthLog.setCreateTime(date);
        return driverMaterialAuthLog;
    }
}
